
import {
  Controller,
  Post,
  Body,
  HttpException,
  HttpStatus,
  UsePipes,
  ValidationPipe,
  UseGuards
} from '@nestjs/common';
import {
  IsInt,
  Min,
  ValidateNested
} from 'class-validator';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { InventoryService } from './inventory.service';
import { InventoryOperationType } from '../entity/inventory-log.entity';
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';
export class DeductStockDTO {
  @IsInt()
  @Type(() => Number)
  skuId!: number;

  @IsInt()
  @Min(1)
  @Type(() => Number)
  quantity!: number;
}

export class RestockDTO {
  @IsInt()
  @Type(() => Number)
  skuId!: number;

  @IsInt()
  @Min(1)
  @Type(() => Number)
  quantity!: number;
}

export class CreateInventoryLogDto {
  @IsInt()
  @Type(() => Number)
  skuId!: number;

  @IsInt()
  @Type(() => Number)
  quantity!: number;

  operationType!: InventoryOperationType;

  notes?: string;
}

class InventoryError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'InventoryError';
  }
}

@Controller('inventory')
export class InventoryController {
  constructor(private readonly inventoryService: InventoryService) {}

  @Post('deduct')
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: '扣减商品库存' })
  @ApiResponse({ status: 200, description: '库存扣减成功' })
  @UsePipes(new ValidationPipe({ transform: true }))
  async deductStock(@Body() dto: DeductStockDTO) {
    try {
      await this.inventoryService.adjustStock(dto.skuId, -dto.quantity);
      return { success: true };
    } catch (e) {
      if (e instanceof InventoryError) {
        throw new HttpException(
          e.message,
          HttpStatus.UNPROCESSABLE_ENTITY
        );
      }
      throw new HttpException(
        '库存操作失败',
        HttpStatus.INTERNAL_SERVER_ERROR
      );
    }
  }

  @Post('restock')
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: '补充商品库存' })
  @ApiResponse({ status: 200, description: '库存补充成功' })
  @UsePipes(new ValidationPipe({ transform: true }))
  async restock(@Body() dto: RestockDTO) {
    console.log(1111)
    try {
      await this.inventoryService.adjustStock(dto.skuId, dto.quantity);
      return { success: true };
    } catch (e) {
      if (e instanceof InventoryError) {
        throw new HttpException(
          e.message,
          HttpStatus.UNPROCESSABLE_ENTITY
        );
      }
      throw new HttpException(
        '库存操作失败',
        HttpStatus.INTERNAL_SERVER_ERROR
      );
    }
  }

  @Post('adjust')
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: '调整库存' })
  @ApiResponse({ status: 200, description: '库存调整成功' })
  @UsePipes(new ValidationPipe({ transform: true }))
  async adjustInventory(@Body() dto: CreateInventoryLogDto) {
    try {
      return await this.inventoryService.adjustInventory(dto);
    } catch (e) {
      if (e instanceof InventoryError) {
        throw new HttpException(
          e.message,
          HttpStatus.UNPROCESSABLE_ENTITY
        );
      }
      throw new HttpException(
        '库存操作失败',
        HttpStatus.INTERNAL_SERVER_ERROR
      );
    }
  }
}

